home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / choice.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  1KB  |  82 lines

  1. /************************************************************************
  2.  * $Id: choice.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *  Routines used to parse options.
  8.  ***********************************************************************/
  9. #if !defined(lint) && defined(F_ID)
  10. char *id_choice = "$Id: choice.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  11. #endif
  12.  
  13. #include <string.h>
  14. #include "ulib.h"
  15.  
  16. static int sep = '|';
  17. static char ssep[2];
  18.  
  19. void
  20. set_sep_letter(int c)
  21. {
  22.     ssep[0] = sep = c;
  23. }
  24.  
  25. int
  26. sep_choices(char *choices, char *obuf[])
  27. {
  28.     register char *c = choices;
  29.     register int n = 0;
  30.     register char *entry = obuf[0];
  31.  
  32.     ssep[0] = sep;
  33.     while (*c)
  34.       {
  35.       if (*c != sep)
  36.         {
  37.         if (*c != '\n')
  38.             *entry++ = *c;
  39.         }
  40.       else
  41.         {
  42.         *entry = '\0';
  43.         entry = obuf[++n];
  44.         }
  45.       c++;
  46.       }
  47.     *entry = '\0';
  48.     return ++n;
  49. }
  50.  
  51. const char *
  52. comb_choices(char *inbuf[], int n)
  53. {
  54.     static char localstr[1024];
  55.     int i;
  56.  
  57.     ssep[0] = sep;
  58.     strcpy(localstr, inbuf[0]);
  59.     for (i = 1; i < n && inbuf[i]; i++)
  60.     strcat(strcat(localstr, ssep), inbuf[i]);
  61.     return localstr;
  62. }
  63.  
  64. #ifdef TEST
  65. #include <stdio.h>
  66. main()
  67. {
  68.     char ll[1024], pool[100][20];
  69.     char *p[100];
  70.     int i, k;
  71.  
  72.     for (i = 0; i < 100; i++)
  73.     p[i] = pool[i];
  74.  
  75.     while (fgets(ll, 1024, stdin))
  76.       {
  77.       printf("total %d\n", k = sep_choices(ll, p));
  78.       printf("%s\n", comb_choices(p, k));
  79.       }
  80. }
  81. #endif
  82.